package com.hnkywl.sts.webservice.wly.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hnkywl.sts.entity.organ.Ck;
import com.hnkywl.sts.service.base.WebserviceLogService;
import com.hnkywl.sts.service.organ.CkService;
import com.hnkywl.sts.webservice.wly.IWarehousingFeedBack;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * Created by Jerry on 14-1-2.
 * 接收新增的仓库信息
 */
@Service
public class WarehousingFeedBackService implements IWarehousingFeedBack, ApplicationContextAware {
    private ApplicationContext applicationContext;

    //仓库Service
    private CkService ckService;
    private WebserviceLogService webserviceLogService;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        ckService = (CkService) applicationContext.getBean("ckService");
        webserviceLogService = (WebserviceLogService) applicationContext.getBean("webserviceLogService");
    }

    @Override
    public String warehousingFeedBackResponse(String json) {
        Gson gson = new Gson();
        StringBuffer sb = new StringBuffer();
        if (json != null) {
            List<Map<String, Object>> resultList = gson.fromJson(json, new TypeToken<List<Map<String, Object>>>() {
            }.getType());
            for (Map<String, Object> resultMap : resultList) {
                try {
                    Ck ck = new Ck();
                    ck.setBh((String) resultMap.get("code"));
                    ck.setMc((String) resultMap.get("name"));
                    ck.setDz((String) resultMap.get("address"));
                    ck.setLxdh((String) resultMap.get("tel"));
                    ck.setDeleted(0);
                    ck.setCkjb(1);
                    ck.setDateCreated(new Date());
                    ck.setLastUpdated(ck.getDateCreated());
                    this.ckService.saveCk(ck);
                    sb.append("{\"code\":\"0\",\"message\":\"仓库创建成功\"}");
                } catch (Exception e) {
                    sb.append("{\"code\":\"1\",\"message\":\"仓库创建失败\"}");
                }
            }

        } else
            sb.append("{\"code\":\"1\",\"message\":\"传入参数为空\"}");
        webserviceLogService.saveClientLog(
                WarehousingFeedBackService.class.getName(),
                "warehousingFeedBackResponse",
                json,
                sb.toString(),
                null,
                ""
        );
        return sb.toString();
    }
}
